package com.ruoyi.mes.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.domain.MesDispatch;
import com.ruoyi.mes.service.IMesDispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.mes.domain.MesDispJobDone;
import com.ruoyi.mes.service.IMesDispJobDoneService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 完工汇报Controller
 * 
 * @author ruoyi
 * @date 2021-11-17
 */
@RestController
@RequestMapping("/done")
public class MesDispJobDoneController extends BaseController
{
    @Autowired
    private IMesDispJobDoneService mesDispJobDoneService;
    @Autowired
    private IMesDispatchService mesDispatchService;

    /**
     * 查询完工汇报列表
     */
    @PreAuthorize(hasPermi = "mes:done:list")
    @GetMapping("/list")
    public TableDataInfo list(MesDispJobDone mesDispJobDone)
    {
        startPage();
        List<MesDispJobDone> list = mesDispJobDoneService.selectMesDispJobDoneList(mesDispJobDone);
        return getDataTable(list);
    }

    /**
     * 导出完工汇报列表
     */
    @PreAuthorize(hasPermi = "mes:done:export")
    @Log(title = "完工汇报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesDispJobDone mesDispJobDone) throws IOException
    {
        List<MesDispJobDone> list = mesDispJobDoneService.selectMesDispJobDoneList(mesDispJobDone);
        ExcelUtil<MesDispJobDone> util = new ExcelUtil<MesDispJobDone>(MesDispJobDone.class);
        util.exportExcel(response, list, "完工汇报数据");
    }

    /**
     * 获取完工汇报详细信息
     */
    @PreAuthorize(hasPermi = "mes:done:query")
    @GetMapping(value = "/{jobDoneUuid}")
    public AjaxResult getInfo(@PathVariable("jobDoneUuid") String jobDoneUuid)
    {
        return AjaxResult.success(mesDispJobDoneService.selectMesDispJobDoneByJobDoneUuid(jobDoneUuid));
    }

    /**
     * 新增完工汇报
     */
    @PreAuthorize(hasPermi = "mes:done:add")
    @Log(title = "完工汇报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesDispJobDone mesDispJobDone)
    {
        // 获取当前汇报数量
        BigDecimal curQty = mesDispJobDone.getDoneQty();

        // 获取剩余可汇报数量
        BigDecimal remainQty = mesDispJobDoneService.selectRemainJobDoneQty(mesDispJobDone);
        // 剩余可汇报数量和当前汇报数量做比较
        if(curQty.compareTo(remainQty)>0){
            return new AjaxResult(1501,"您提交的实际汇报数量为"+curQty+","+"当前剩余实际可汇报数量为"+remainQty+"!");
        }else{
            return toAjax(mesDispJobDoneService.insertMesDispJobDone(mesDispJobDone));
        }
    }

    /**
     * 修改完工汇报
     */
    @PreAuthorize(hasPermi = "mes:done:edit")
    @Log(title = "完工汇报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesDispJobDone mesDispJobDone)
    {
        // 获取当前汇报数量
        BigDecimal curQty = mesDispJobDone.getDoneQty();

        // 获取剩余可汇报数量
        BigDecimal remainQty = mesDispJobDoneService.selectRemainJobDoneQty(mesDispJobDone);
        // 剩余可汇报数量和当前汇报数量做比较
        if(curQty.compareTo(remainQty)>0){
            return new AjaxResult(1521,"您提交的实际汇报数量为"+curQty+","+"当前剩余实际可汇报数量为"+remainQty+"!");
        }else{
            return toAjax(mesDispJobDoneService.updateMesDispJobDone(mesDispJobDone));
        }

    }

    /**
     * 删除完工汇报
     */
    @PreAuthorize(hasPermi = "mes:done:remove")
    @Log(title = "完工汇报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jobDoneUuids}")
    public AjaxResult remove(@PathVariable String[] jobDoneUuids)
    {
        return toAjax(mesDispJobDoneService.deleteMesDispJobDoneByJobDoneUuids(jobDoneUuids));
    }
}
